package com.kvw.auth.center.service;

import com.kt.kvw.model.domain.KvwUser;
import com.kvw.auth.center.dao.UserAuthDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Desc:
 *
 * @author: keen
 * Date: 2020-08-17
 * Time: 22:36
 */
@Service
public class KvwUserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserAuthDao userAuthDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        KvwUser kvwUser = userAuthDao.selectByUsername(username);
        if (kvwUser == null) {
            throw new UsernameNotFoundException("用户" + username + "不存在");
        }
        return new User(username, passwordEncoder.encode(kvwUser.getPassword()),
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
